package com.niiwoo.civet.trade.service.local.newRefund.notify.mq;

import com.alibaba.fastjson.JSON;
import com.niiwoo.asset.org.business.dto.mq.RepaymentSuccessDTO;
import com.niiwoo.civet.trade.dao.entity.*;
import com.niiwoo.civet.trade.dao.mapper.ProjectBorrowerPlanNewMapperExt;
import com.niiwoo.civet.trade.dao.mapper.ProjectBorrowerSummaryMapperExt;
import com.niiwoo.civet.trade.dao.mapper.RefundOrderDetailBizMapperExt;
import com.niiwoo.civet.trade.enums.RepayStatusEnum;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyDTO;
import com.niiwoo.civet.trade.service.local.newRefund.notify.enums.RefundNotifyEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * 机构标的还款MQ通知
 * 
 * 触发条件：还款成功
 * 
 * @author: kevin.jia
 * @version 1.0.0
 */
@Slf4j
@Component
public class OrgRefundResultNotify extends MQRefundNotify {
	@Autowired
	private RefundOrderDetailBizMapperExt refundOrderDetailMapperExt;
	@Autowired
	private ProjectBorrowerPlanNewMapperExt projectBorrowerPlanNewMapperExt;
	@Autowired
	private ProjectBorrowerSummaryMapperExt projectBorrowerSummaryMapperExt;

	@Override
	public Object bulidParam(RefundNotifyDTO refundNotifyDTO) {
		RefundOrder refundOrder = refundNotifyDTO.getRefundOrder();
		
		RepaymentSuccessDTO repaymentSuccessDTO = new RepaymentSuccessDTO();
		repaymentSuccessDTO.setRefundOrderId(refundOrder.getId());
		repaymentSuccessDTO.setProjectId(refundOrder.getProjectId());
		repaymentSuccessDTO.setRequestNo(refundOrder.getOrgRequestNo());
		repaymentSuccessDTO.setAmount(refundOrder.getAmount());
		repaymentSuccessDTO.setRefundingAmount(refundOrder.getRefundingAmount());
		repaymentSuccessDTO.setCapital(refundOrder.getCapital());
		repaymentSuccessDTO.setInterest(refundOrder.getInterest());
		repaymentSuccessDTO.setPenaltyFee(refundOrder.getPenaltyFee());
		repaymentSuccessDTO.setManageFee(refundOrder.getManageFee());
		repaymentSuccessDTO.setConsultingFee(refundOrder.getConsultingFee());
		repaymentSuccessDTO.setGuaranteeFee(refundOrder.getGuaranteeFee());
		repaymentSuccessDTO.setTradeSource(refundOrder.getTradeSource());
		repaymentSuccessDTO.setRefundType(refundOrder.getRefundType());
		repaymentSuccessDTO.setOrgId(refundOrder.getOrgId());
		repaymentSuccessDTO.setErrorCode(refundOrder.getErrorCode());
		repaymentSuccessDTO.setErrorMsg(refundOrder.getErrorMessage());
		repaymentSuccessDTO.setPeriods(this.getBorrowerPeriods(refundNotifyDTO));
		//ProjectBorrowerPlanNew borrowerPlan = this.getBorrowerPlanId(refundNotifyDTO);
		//repaymentSuccessDTO.setBorrowPlanIdStart(borrowerPlan == null? null:borrowerPlan.getId());
		ProjectBorrowerSummary summary = projectBorrowerSummaryMapperExt.selectByPrimaryKey(refundNotifyDTO.getProjectId());
		repaymentSuccessDTO.setNextRefundDate(summary.getRepayStatus().equals(RepayStatusEnum.SETTLEMENT.getCode())? null:summary.getRefundDate());
		repaymentSuccessDTO.setSuccessTime(refundOrder.getSuccessTime());
		repaymentSuccessDTO.setStatus(refundOrder.getStatus());
		log.debug("civet-trade:发送还款消息repaymentSuccessDTO={}", JSON.toJSONString(repaymentSuccessDTO));
		return repaymentSuccessDTO;
	}

	private String  getBorrowerPeriods(RefundNotifyDTO refundNotifyDTO){
		List<RefundOrderDetailBiz> refundOrderDetailBizList = refundNotifyDTO.getRefundOrderDetailBizList();
		Set<Integer> setPeriods = new TreeSet<>((o1, o2) -> {
			if (o1>o2){
				return 1;
			}else if (o1<o2){
				return -1;
			}
			return 0;
		});
		for (RefundOrderDetailBiz biz : refundOrderDetailBizList) {
			if (biz.getCurrentPeriod()>0 ) {
				setPeriods.add(biz.getCurrentPeriod());
			}
		}
		return JSON.toJSONString(setPeriods).replaceAll("\\[","").replaceAll("]","");
	}

	@Override
	public RefundNotifyEnum getType() {
		return RefundNotifyEnum.ORG_REFUND_RESULT;
	}
}